﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using Cotizav.CONEXION;
using System.Windows.Forms;

namespace Cotizav
{
    public class CtrlCargo
    {
        private ConexionDataContext conexion;

        internal void registrarCargo(CARGO cargo)
        {
            using (TransactionScope transactionScope = new TransactionScope())
            {
                using (conexion = new ConexionDataContext())
                {
                    try
                    {
                        int correlativoCargo = conexion.COTIZAV_CORRELATIVO.Single(x => x.IDENTIFICADOR == "COTIZAV").CARGO.Value;
                        string num = Convert.ToString(correlativoCargo);
                        string numero = num.Trim().PadLeft(3, '0');
                        string idCargo = "IDCARGO_" + numero;

                        cargo.idCargo = idCargo;
                        cargo.eliminado = 0;

                        //insertamos
                        conexion.CARGO.InsertOnSubmit(cargo);
                        conexion.SubmitChanges();
                        conexion.increm_ContCargo();

                        transactionScope.Complete();                        
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                } 
            }
        }

        internal List<CONEXION.CARGO> getListCargos()
        {
            using (conexion = new ConexionDataContext())
            {
                var query = conexion.CARGO.Where(x => x.eliminado == 0).OrderBy( x=> x.nombreCargo).ToList<CARGO>();
                return query;
            }
        }

        internal System.Windows.Forms.DialogResult actualizarCargo(CARGO car)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        CONEXION.CARGO carg = conexion.CARGO.Single(x => x.idCargo == car.idCargo);
                        DialogResult result = DialogResult.Cancel;

                        carg.nombreCargo = car.nombreCargo;
                        carg.eliminado = car.eliminado;

                        conexion.SubmitChanges();
                        transactionScope.Complete();
                        result = DialogResult.OK;

                        return result;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        internal void eliminarCargo(CARGO car)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        CONEXION.CARGO carg = conexion.CARGO.Single(x => x.idCargo == car.idCargo);
                        //actualizamos
                        carg.eliminado = car.eliminado;

                        conexion.SubmitChanges();
                        transactionScope.Complete();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }
    }
}
